package com.tuanzi.controller.admin;

import com.tuanzi.ResultVo.ResultVO;
import com.tuanzi.entity.User;
import com.tuanzi.service.PwdService;
import com.tuanzi.utils.Status;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author tuanzi
 * @version 1.0
 * @date 2022/3/31 13:24
 */

@RestController
@RequestMapping("/admin")
public class ChangePwdController {

    @Resource
    private PwdService pwdService;

    /**
     * 更改密码
     */
    @RequestMapping(value = "/pwd",method = RequestMethod.POST)
    public ResultVO changePwd(@RequestParam String oldPwd, @RequestParam String newPwd,
                               HttpSession session){

        User user = (User) session.getAttribute("user");
        System.out.println(user);
        if (!user.getPassword().equals(oldPwd)) {
            // 如果旧密码和session中保存的不一致，拒绝保存
            return new ResultVO(Status.CHANGEFAILED,null);
        }
        ResultVO vo = pwdService.updatePwd(user.getId(), newPwd);
        // 更新保存的cookie
        user.setPassword(newPwd);
        session.setAttribute("user",user);

        return vo;
    }

}
